<template>
  <el-dialog
    :title="!dataForm.bhId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="书名id" prop="bhBookId">
      <el-input v-model="dataForm.bhBookId" placeholder="书名id"></el-input>
    </el-form-item>
    <el-form-item label="书签id" prop="bhLabelId">
      <el-input v-model="dataForm.bhLabelId" placeholder="书签id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="bhCreationTime">
      <el-input v-model="dataForm.bhCreationTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="bhUpdateTime">
      <el-input v-model="dataForm.bhUpdateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="历史状态 1-未加入书架,1-加入书架" prop="bhStatus">
      <el-input v-model="dataForm.bhStatus" placeholder="历史状态 1-未加入书架,1-加入书架"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bhId: 0,
          bhBookId: '',
          bhLabelId: '',
          bhCreationTime: '',
          bhUpdateTime: '',
          bhStatus: ''
        },
        dataRule: {
          bhBookId: [
            { required: true, message: '书名id不能为空', trigger: 'blur' }
          ],
          bhLabelId: [
            { required: true, message: '书签id不能为空', trigger: 'blur' }
          ],
          bhCreationTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          bhUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          bhStatus: [
            { required: true, message: '历史状态 1-未加入书架,1-加入书架不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bhId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bhId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbbrowsinghistory/info/${this.dataForm.bhId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.bhBookId = data.tbBrowsingHistory.bhBookId
                this.dataForm.bhLabelId = data.tbBrowsingHistory.bhLabelId
                this.dataForm.bhCreationTime = data.tbBrowsingHistory.bhCreationTime
                this.dataForm.bhUpdateTime = data.tbBrowsingHistory.bhUpdateTime
                this.dataForm.bhStatus = data.tbBrowsingHistory.bhStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tbbrowsinghistory/${!this.dataForm.bhId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bhId': this.dataForm.bhId || undefined,
                'bhBookId': this.dataForm.bhBookId,
                'bhLabelId': this.dataForm.bhLabelId,
                'bhCreationTime': this.dataForm.bhCreationTime,
                'bhUpdateTime': this.dataForm.bhUpdateTime,
                'bhStatus': this.dataForm.bhStatus
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
